﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using OpenExpressApp;
using OpenExpressApp.Module.WPF;
using System.ComponentModel;
using OpenExpressApp.Module.Library.Security;
using OpenExpressApp.MetaModel;
using System.Diagnostics;
using System.Windows.Automation;
using System.ComponentModel.Composition;
using System.Windows.Controls.Primitives;

namespace OpenExpressApp.WPF.SystemModule
{
    /// <summary>
    /// 软件界面左边的模块列表界面
    /// </summary>
    [Export(ContractName.Tools, typeof(Control))]
    [Export(ContractName.ModuleListPad, typeof(Control))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public partial class ModuleListPad : UserControl
    {
        public ModuleListPad()
        {
            InitializeComponent();


            this.BindToListBox();

            //设置Group样式
            var gs = new GroupStyle();
            gs.ContainerStyle = Application.Current.TryFindResource("GroupContainerStyle") as Style;
            lbModule.GroupStyle.Add(gs);

            this.OpenDefaultOpenModule();
        }

        [Export(ContractName.ModuleListBox, typeof(Selector))]
        private Selector ModuleListBox
        {
            get { return lbModule; }
        }

        /// <summary>
        /// 绑定Module到视图
        /// </summary>
        private void BindToListBox()
        {
            var modules = GetReadableModules();

            var collectionView = CollectionViewSource.GetDefaultView(modules);
            collectionView.GroupDescriptions.Add(new PropertyGroupDescription("Catalog"));
            collectionView.SortDescriptions.Add(new SortDescription("Index", ListSortDirection.Ascending));

            var itemsBinding = new Binding() { Source = collectionView };
            lbModule.SetBinding(ListBox.ItemsSourceProperty, itemsBinding);
        }

        /// <summary>
        /// 打开需要缺省打开的模块
        /// </summary>
        /// <param name="modules"></param>
        private void OpenDefaultOpenModule()
        {
            int n = 0;
            while (n < lbModule.Items.Count)
            {
                BusinessObjectInfo objectinfo = lbModule.Items[n] as BusinessObjectInfo;
                if ((objectinfo.DefaultObjectAttribute != null) && (objectinfo.DefaultObjectAttribute.DefaultOpen))
                {
                    lbModule.SelectedItem = lbModule.Items[n];
                    this.ShowModule();
                    break;
                }
                n = n + 1;
            }
        }

        private void ShowModule()
        {
            if (lbModule.SelectedItem != null)
            {
                BusinessObjectInfo info = (BusinessObjectInfo)lbModule.SelectedItem;
                //如果已经打开则激活模块，否则新增模块窗体
                FrameworkElement fe = OpenApplication.OpenModule(info.BOType) as FrameworkElement;
                //支持UI Test
                fe.SetValue(AutomationProperties.AutomationIdProperty, "module:" + info.Id.ToString());
            }
        }

        /// <summary>
        /// 获取模块列表数据源
        /// </summary>
        /// <returns></returns>
        private static IList<BusinessObjectInfo> GetReadableModules()
        {
            IList<BusinessObjectInfo> modules = new List<BusinessObjectInfo>();
            if (Csla.ApplicationContext.User.Identity.IsAuthenticated)
            {
                foreach (var item in ApplicationModel.GetAllModules())
                {
                    var curIdentity = Csla.ApplicationContext.User.Identity as OEAIdentity;
                    if (curIdentity.HavePermissionOnOperation(item.Id, new Guid(CommandNames.CanViewObject)))
                    {
                        modules.Add(item);
                    }
                }
            }
            return modules;
        }

        private void lbModule_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.ShowModule();
        }

        private void lbModule_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            this.ShowModule();
        }
    }
}
